package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "04. 分类管理模块")
@RestController
@RequestMapping("/categories")
@Slf4j
public class CategoryController {

    @Autowired
    ICategoryService service;

    //    http://localhost:9080/categories/add-new?name=测试类别03&parentId=24&keywords=TestKeywords&sort=23&icon=TestIcon&enable=1&isDisplay=1
    @ApiOperation("添加分类")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@RequestBody CategoryAddNewDTO categoryAddNewDTO){
        log.debug("开始处理添加类别请求");
        service.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    //    http://localhost:9080/categories/delete?id=84
    @ApiOperation("删除分类")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除类别请求");
        service.delete(id);
        return JsonResult.ok();
    }

    //    http://localhost:9080/categories
    @ApiOperation("查询一级父级列表列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<CategoryListItemVO>> getListOnlyParent(){
        log.debug("开始处理查询一级父级类别列表的请求");
        List<CategoryListItemVO> list = service.getListOnlyParent();
        return JsonResult.ok(list);
    }

    @ApiOperation("查询子级列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("/child/{id:[0-9]+}")
    public JsonResult<List<CategoryListItemVO>> getListChild(@PathVariable Long id){
        log.debug("开始处理查询子级类别列表的请求");
        return JsonResult.ok(service.getListByParentId(id));
    }
}
